package com.taozi.netty.socketio.use.classroomchat.service.socketio;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import com.taozi.netty.socketio.use.classroomchat.constant.EducationConstant;
import com.taozi.netty.socketio.use.classroomchat.entity.entity.education.ClassroomUser;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomUserMapper;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ClassroomChatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * description 登录课堂监听
 *
 * @author mingtao
 * date：2019/7/1 16:33
 */
@Service("loginChatListener")
@Slf4j
public class LoginChatListener implements DataListener<ClassroomChatVo> {

    @Autowired
    ClassroomUserMapper classroomUserMapper;

    SocketIOServer socketIOServer;
    public void setServer(SocketIOServer server) {
        this.socketIOServer = server;
    }

    @Override
    public void onData(SocketIOClient socketIOClient, ClassroomChatVo classroomChatVo, AckRequest ackRequest) throws Exception {

        log.info("登录信息：" + classroomChatVo.toString());
        if(classroomChatVo == null || classroomChatVo.getClassroomId() == null || classroomChatVo.getUserId() == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("参数缺失");
            }
            return;
        }
        //登录成功
        addOrUpdateClassroomUser(classroomChatVo);
        log.info("登录成功。。。");
        //添加命名空间
        SocketIONamespace chatNamespace = socketIOServer.addNamespace("/" + classroomChatVo.getClassroomId());
        chatNamespace.addEventListener("message", ClassroomChatVo.class, new DataListener<ClassroomChatVo>() {
            @Override
            public void onData(SocketIOClient socketIOClient, ClassroomChatVo classroomChatVo, AckRequest ackRequest) throws Exception {
                log.info("message  " + classroomChatVo.toString());
                chatNamespace.getBroadcastOperations().sendEvent("message", classroomChatVo);
            }
        });

        //发送加入消息
        classroomChatVo.setMsgContent(classroomChatVo.getUserName() + "加入课堂");
        socketIOServer.getBroadcastOperations().sendEvent("login" + classroomChatVo.getClassroomId().toString(), classroomChatVo);


        //根据课堂id查询课堂在线人员
        classroomChatVo.setIsOnline(EducationConstant.STATUS_YES);
        List<ClassroomUser> classroomUserList = classroomUserMapper.selectByClassroomId(classroomChatVo);

        //发送新的在线人员列表
        socketIOServer.getBroadcastOperations().sendEvent("onlineUser" + classroomChatVo.getClassroomId().toString(), classroomUserList);

    }

    /**
    * @description 添加或修改
    * @param
    * @return void
    * @author mingtao
    * @date 16:45 2019/7/1
    **/
    private void addOrUpdateClassroomUser(ClassroomChatVo classroomChatVo){

        if(classroomChatVo == null || classroomChatVo.getClassroomId() == null || classroomChatVo.getUserId() == null){
            return ;
        }

        //查询人员是否存在，存在只修改在线状态
        ClassroomUser classroomUser = classroomUserMapper.selectByUserIdAndClassroomId(classroomChatVo);
        if(classroomUser == null){ //添加
            classroomUser = new ClassroomUser();
            classroomUser.setClassroomId(classroomChatVo.getClassroomId());
            classroomUser.setUserId(classroomChatVo.getUserId());
            classroomUser.setUserName(classroomChatVo.getUserName());
            classroomUser.setIsLecturer(EducationConstant.STATUS_NO);
            classroomUser.setIsOnline(EducationConstant.STATUS_YES);
            classroomUser.setIsBanChat(EducationConstant.ALLOWED_CHAT);
            classroomUser.setCreateTime(new Date());
            classroomUser.setUpdateTime(new Date());
            classroomUserMapper.insertSelective(classroomUser);
        }else { //修改
            if(classroomUser.getIsOnline() == EducationConstant.STATUS_NO){
                classroomUser.setUpdateTime(new Date());
                classroomUser.setIsOnline(EducationConstant.STATUS_YES);
                classroomUserMapper.updateByPrimaryKeySelective(classroomUser);
            }
        }

    }


}
